package leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution46 {
    List<List<Integer>> result = new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {
        LinkedList<Integer> temp = new LinkedList<>();
        int[] used = new int[nums.length];
        backtrack(nums,used,temp);
        return result;
    }

    private void backtrack(int[] nums, int[] used,LinkedList<Integer> temp) {
        if(temp.size() == nums.length){
            result.add(new LinkedList<>(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(used[i]==1){
                continue;
            }
            temp.add(nums[i]);
            used[i]=1;
            backtrack(nums,used,temp);
            temp.removeLast();
            used[i]=0;
        }
    }
}
